home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libobj / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.2 KB  |  156 lines

  1. /*
  2.  * list.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * list.c,v 4.1 1994/08/09 07:59:31 explorer Exp
  17.  *
  18.  * list.c,v
  19.  * Revision 4.1  1994/08/09  07:59:31  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:09  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:38:42  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "geom.h"
  30. #include "list.h"
  31.  
  32. static Methods *iListMethods = NULL;
  33. static char listName[] = "list";
  34.  
  35. List *
  36. ListCreate()
  37. {
  38.     return (List *)share_calloc(1, sizeof(List));
  39. }
  40.  
  41. char *
  42. ListName()
  43. {
  44.     return listName;
  45. }
  46.  
  47. /*
  48.  * Take a list whose DATA field points to a linked list of objects and
  49.  * turn it into a List.
  50.  */
  51. int
  52. ListConvert(list, objlist)
  53. List *list;
  54. Geom *objlist;
  55. {
  56.     int num;
  57.  
  58.     /*
  59.      * Find the unbounded objects on the list as well as the
  60.      * bounding box of the list.
  61.      */
  62.     list->list = objlist;
  63.     for (num = 0; objlist; objlist = objlist->next)
  64.         num += objlist->prims;
  65.     return num;
  66. }
  67.  
  68. /*
  69.  * Intersect ray & list of objects.
  70.  */
  71. int
  72. ListIntersect(list, ray, hitlist, mindist, maxdist)
  73. List *list;
  74. Ray *ray;
  75. HitList *hitlist;
  76. Float mindist, *maxdist;
  77. {
  78.     Geom *objlist;
  79.     Vector vtmp;
  80.     Float s;
  81.     int hit;
  82.  
  83.     hit = FALSE;
  84.     /*
  85.      * Intersect with unbounded objects.
  86.      */
  87.     for (objlist = list->unbounded; objlist ; objlist = objlist->next) {
  88.         if (intersect(objlist, ray, hitlist, mindist, maxdist))
  89.             hit = TRUE;
  90.     }
  91.  
  92.     /*
  93.      * Check for intersection with bounding box.
  94.      */
  95.     s = *maxdist;    /* So maxdist won't be reset. */
  96.     VecAddScaled(ray->pos, mindist, ray->dir, &vtmp);
  97.     if (OutOfBounds(&vtmp, list->bounds) &&
  98.         !BoundsIntersect(ray, list->bounds, mindist, &s))
  99.         /*
  100.          * Ray never hit list.
  101.          */
  102.         return hit;
  103.     /*
  104.      * Else the ray enters list-space before it hits an
  105.      * unbounded object. Intersect with objects on list.
  106.      */
  107.     for (objlist = list->list; objlist ; objlist = objlist->next) {
  108.         if (intersect(objlist, ray, hitlist, mindist, maxdist))
  109.             hit = TRUE;
  110.     }
  111.  
  112.     return hit;
  113. }
  114.  
  115. Methods *
  116. ListMethods()
  117. {
  118.     if (iListMethods == (Methods *)NULL) {
  119.         iListMethods = MethodsCreate();
  120.         iListMethods->methods = ListMethods;
  121.         iListMethods->create = (GeomCreateFunc *)ListCreate;
  122.         iListMethods->name = ListName;
  123.         iListMethods->intersect = ListIntersect;
  124.         iListMethods->bounds = ListBounds;
  125.         iListMethods->convert = ListConvert;
  126.         iListMethods->checkbounds = FALSE;
  127.         iListMethods->closed = TRUE;
  128.     }
  129.     return iListMethods;
  130. }
  131.  
  132. void
  133. ListBounds(list, bounds)
  134. List *list;
  135. Float bounds[2][3];
  136. {
  137.     Geom *obj, *next;
  138.  
  139.     BoundsInit(list->bounds);
  140.     /*
  141.      * For each object on the list,
  142.      * compute its bounds...
  143.      */
  144.     list->unbounded  = GeomComputeAggregateBounds(&list->list, 
  145.                 list->unbounded, list->bounds);
  146.     BoundsCopy(list->bounds, bounds);
  147. }
  148.  
  149. void
  150. ListMethodRegister(meth)
  151. UserMethodType meth;
  152. {
  153.     if (iListMethods)
  154.         iListMethods->user = meth;
  155. }
  156.